package id.co.rho.scoring.cid.client.widgets;

import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.HeaderGroupConfig;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Hidden;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: frizky
 * Date: 10/2/11
 * Time: 8:20 PM
 */
public class GridExcel<M extends ModelData> extends FormPanel {

    private Grid<M> grid;
    private ToolBar toolBar;
    private Button btnExport;

    public GridExcel(ListStore<M> mListStore, ColumnModel columnModel) {
        grid = new Grid<M>(mListStore, columnModel);
    }

    public GridExcel(ListStore<M> mListStore, ColumnModel columnModel, Button btnExport) {
        this.btnExport = btnExport;
        grid = new Grid<M>(mListStore, columnModel);
    }

    @Override
    protected void beforeRender() {
        super.beforeRender();
        setHeaderVisible(false);
//        if (btnExport == null) {
        toolBar = new ToolBar();
        setTopComponent(toolBar);
        btnExport = new Button("Export to Excel");
        toolBar.add(btnExport);
        toolBar.setAlignment(Style.HorizontalAlignment.RIGHT);
//        }
    }

    @Override
    protected void onRender(Element target, int index) {
        super.onRender(target, index);
        setAction(GWT.getModuleBaseURL() + "exportExcel");
        setMethod(Method.POST);
        setFrame(true);
        setScrollMode(Style.Scroll.AUTOY);
        setCollapsible(false);
        setAnimCollapse(false);
        setButtonAlign(Style.HorizontalAlignment.CENTER);
        setHeading("Search Result");
        setLayout(new FitLayout());
        final Hidden headerHidden = new Hidden("header");
        final Hidden fieldsHidden = new Hidden("fields");
        final Hidden contentHidden = new Hidden("content");
        add(headerHidden);
        add(fieldsHidden);
        add(contentHidden);
        btnExport.addSelectionListener(new SelectionListener<ButtonEvent>() {
            @Override
            public void componentSelected(ButtonEvent ce) {
                ListStore store1 = grid.getStore();
                List<ModelData> models = store1.getModels();
                ColumnModel columnModel = grid.getColumnModel();
                List<ColumnConfig> columns = columnModel.getColumns();
                JSONArray headerArray = new JSONArray();
                JSONArray fieldArray = new JSONArray();
                String[] fields = new String[columns.size()];
                int i = 0;
                for (ColumnConfig columnConfig : columns) {
                    headerArray.set(i, new JSONString(String.valueOf(columnConfig.getHeader())));
                    fieldArray.set(i, new JSONString(String.valueOf(columnConfig.getId())));

                    fields[i] = columnConfig.getId();
                    i++;
                }
                int indexModelsArray = 0;
                JSONArray modelsArray = new JSONArray();
                for (ModelData model : models) {
                    JSONArray modelArray = new JSONArray();
                    int indexModelArray = 0;
                    for (String strField : fields) {
                        modelArray.set(indexModelArray, new JSONString(String.valueOf(model.get(strField))));
                        indexModelArray++;
                    }
                    modelsArray.set(indexModelsArray, modelArray);
                    indexModelsArray++;
                }
                headerHidden.setValue(headerArray.toString());
                fieldsHidden.setValue(fieldArray.toString());
                contentHidden.setValue(modelsArray.toString());
                submit();
            }
        });

        add(grid);
    }

    public Grid<M> getGrid() {
        return grid;
    }

    public void setGrid(Grid<M> grid) {
        this.grid = grid;
    }

    public void hideToolbar(boolean visible) {
        if (toolBar != null && toolBar.isRendered())
            toolBar.setVisible(visible);
    }
}
